﻿using Newtonsoft.Json;
using QLBH.DATA;
using QLBH.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;

namespace QLBH.BUSINESS
{
    /// <summary>
    /// Summary description for Services
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Services : System.Web.Services.WebService
    {
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

        [WebMethod]
        public void test()
        {
            PHIEUNHAPPARAM PHIEUNHAP = new PHIEUNHAPPARAM();
            PHIEUNHAP.MA_KHO_HANG = "KO0001";
            PHIEUNHAP.MA_NHA_CUNG_CAP = "NCC0001";
            PHIEUNHAP.MA_NHAN_VIEN = "NV0001";
            PHIEUNHAP.NGAY_NHAP = DateTime.Now;
            PHIEUNHAP.MA_PHIEU_NHAP = "";

            PHIEUNHAP.CHITIETNHAP = new List<CHITIETNHAPPARAM>();
            CHITIETNHAPPARAM CT = new CHITIETNHAPPARAM()
                {
                    GIA_NHAP = 15000,
                    HAN_SU_DUNG = DateTime.Now,
                    MA_PHIEU_NHAP = "",
                    MA_SAN_PHAM = "SP0002",
                    SO_LUONG = 1
                };
            PHIEUNHAP.CHITIETNHAP.Add(CT);


            PhieuNhapBus.ThemMoi(PHIEUNHAP);
        }

        [WebMethod]
        public string DangNhap(string ten_dang_nhap, string mat_khau)
        {
            string json = "";
            var obj = NguoiDungBus.DangNhap(ten_dang_nhap, mat_khau);


            if (obj == null)
            {
                json = new JavaScriptSerializer().Serialize(new { SUCCESS = -1, ERROR = "Sai tên đăng nhập hoặc mật khẩu" });
                return json;
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = 1, MA_NHAN_VIEN = obj.MA_NHAN_VIEN, TEN_DANG_NHAP = obj.TEN_DANG_NHAP });
            return json;
        }

        [WebMethod]
        public string ThemMoiNguoiDung(NGUOIDUNGPARAM nguoi_dung)
        {
            string json = "";
            int S = -1;

            var obj = NguoiDungBus.ThemMoiNguoiDung(nguoi_dung);
            if (obj == "1")
            {
                S = 1;
                obj = "Đã thêm thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string ThemMoiNhomSanPham(NHOMSANPHAMPARAM NHOM)
        {
            string json = "";
            int S = -1;

            var obj = NhomSanPhamBus.ThemMoi(NHOM);
            if (obj == "1")
            {
                S = 1;
                obj = "Đã thêm thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string ThemMoiKho(KHOHANGPARAM KHO)
        {
            string json = "";
            int S = -1;

            var obj = KhoHangBus.Them(KHO);
            if (obj == "1")
            {
                S = 1;
                obj = "Đã thêm thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string ThemMoiPhongBan(PHONGBANPARAM PHONGBAN)
        {
            string json = "";
            int S = -1;

            var obj = PhongBanBus.ThemMoi(PHONGBAN);
            if (obj == "1")
            {
                S = 1;
                obj = "Đã thêm thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string ThemMoiNhaCungCap(NHACUNGCAPPARAM NHACUNGCAP)
        {
            string json = "";
            int S = -1;

            var obj = NhaCungCapBus.ThemMoi(NHACUNGCAP);
            if (obj == "1")
            {
                S = 1;
                obj = "Đã thêm thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string ThemMoiKhachHang(KHACHHANGPARAM KHACHHANG)
        {
            string json = "";
            int S = -1;

            var obj = KhachHangBus.ThemMoi(KHACHHANG);
            if (obj == "1")
            {
                S = 1;
                obj = "Đã thêm thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string ThemMoiNhanVien(NHANVIENPARAM NHANVIEN)
        {
            string json = "";
            int S = -1;

            var obj = NhanVienBus.ThemMoi(NHANVIEN);
            if (obj == "1")
            {
                S = 1;
                obj = "Đã thêm thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string ThemMoiSanPham(SANPHAMPARAM SANPHAM)
        {
            string json = "";
            int S = -1;

            var obj = SanPhamBus.ThemMoi(SANPHAM);
            if (obj == "1")
            {
                S = 1;
                obj = "Đã thêm thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string ThemMoiPhieuNhap(PHIEUNHAPPARAM PHIEUNHAP)
        {
            string json = "";
            int S = -1;

            var obj = PhieuNhapBus.ThemMoi(PHIEUNHAP);
            string MPN = PhieuNhapBus.MA_PN;
            if (obj == "1")
            {
                S = 1;
                obj = "Đã thêm thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj, MPN = MPN });
            return json;
        }

        [WebMethod]
        public string ThemMoiPhieuXuat(PHIEUXUATPARAM PHIEUXUAT)
        {
            string json = "";
            int S = -1;

            var obj = PhieuXuatBus.ThemMoi(PHIEUXUAT);
            string MPX = PhieuXuatBus.MA_PX;
            if (obj == "1")
            {
                S = 1;
                obj = "Đã thêm thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj, MPX = MPX });
            return json;
        }

        [WebMethod]
        public string CapNhatNguoiDung(NGUOIDUNGPARAM nguoi_dung)
        {
            string json = "";
            int S = -1;

            var obj = NguoiDungBus.CapNhatNguoiDung(nguoi_dung);
            if (obj == "1")
            {
                S = 1;
                obj = "Cập nhật thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string CapNhatNhomSanPham(NHOMSANPHAMPARAM NHOM)
        {
            string json = "";
            int S = -1;

            var obj = NhomSanPhamBus.CapNhat(NHOM);
            if (obj == "1")
            {
                S = 1;
                obj = "Cập nhật thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string CapNhatKho(KHOHANGPARAM KHO)
        {
            string json = "";
            int S = -1;

            var obj = KhoHangBus.CapNhat(KHO);
            if (obj == "1")
            {
                S = 1;
                obj = "Cập nhật thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string CapNhatPhongBan(PHONGBANPARAM PHONGBAN)
        {
            string json = "";
            int S = -1;

            var obj = PhongBanBus.CapNhat(PHONGBAN);
            if (obj == "1")
            {
                S = 1;
                obj = "Cập nhật thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string CapNhatNhaCungCap(NHACUNGCAPPARAM NHACUNGCAP)
        {
            string json = "";
            int S = -1;

            var obj = NhaCungCapBus.CapNhat(NHACUNGCAP);
            if (obj == "1")
            {
                S = 1;
                obj = "Cập nhật thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string CapNhatKhachHang(KHACHHANGPARAM KHACHHANG)
        {
            string json = "";
            int S = -1;

            var obj = KhachHangBus.CapNhat(KHACHHANG);
            if (obj == "1")
            {
                S = 1;
                obj = "Cập nhật thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string CapNhatNhanVien(NHANVIENPARAM NHANVIEN)
        {
            string json = "";
            int S = -1;

            var obj = NhanVienBus.CapNhat(NHANVIEN);
            if (obj == "1")
            {
                S = 1;
                obj = "Cập nhật thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string CapNhatSanPham(SANPHAMPARAM SANPHAM)
        {
            string json = "";
            int S = -1;

            var obj = SanPhamBus.CapNhat(SANPHAM);
            if (obj == "1")
            {
                S = 1;
                obj = "Cập nhật thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string XoaNguoiDung(string[] ma_nguoi_dung)
        {
            //= new string[] { "ND0001,ND0002" };
            string json = "";
            int S = -1;

            var obj = NguoiDungBus.XoaNguoiDung(ma_nguoi_dung);
            if (obj == "1")
            {
                S = 1;
                obj = "Xóa thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string XoaNhomSanPham(string[] ma_nhom_san_pham)
        {
            //= new string[] { "ND0001,ND0002" };
            string json = "";
            int S = -1;

            var obj = NhomSanPhamBus.Xoa(ma_nhom_san_pham);
            if (obj == "1")
            {
                S = 1;
                obj = "Xóa thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string XoaKho(string[] ma_kho_hang)
        {
            //= new string[] { "ND0001,ND0002" };
            string json = "";
            int S = -1;

            var obj = KhoHangBus.Xoa(ma_kho_hang);
            if (obj == "1")
            {
                S = 1;
                obj = "Xóa thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string XoaPhongBan(string[] ma_phong_ban)
        {
            string json = "";
            int S = -1;

            var obj = PhongBanBus.Xoa(ma_phong_ban);
            if (obj == "1")
            {
                S = 1;
                obj = "Xóa thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string XoaNhaCungCap(string[] ma_nha_cung_cap)
        {
            string json = "";
            int S = -1;

            var obj = NhaCungCapBus.Xoa(ma_nha_cung_cap);
            if (obj == "1")
            {
                S = 1;
                obj = "Xóa thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string XoaKhachHang(string[] ma_khach_hang)
        {
            string json = "";
            int S = -1;

            var obj = KhachHangBus.Xoa(ma_khach_hang);
            if (obj == "1")
            {
                S = 1;
                obj = "Xóa thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string XoaNhanVien(string[] ma_nhan_vien)
        {
            string json = "";
            int S = -1;

            var obj = NhanVienBus.Xoa(ma_nhan_vien);
            if (obj == "1")
            {
                S = 1;
                obj = "Xóa thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string XoaSanPham(string[] ma_san_pham)
        {
            string json = "";
            int S = -1;

            var obj = SanPhamBus.Xoa(ma_san_pham);
            if (obj == "1")
            {
                S = 1;
                obj = "Xóa thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string TimNguoiDungTheoTen(string ten_dang_nhap, int tong_so_dong)
        {
            string json = "";
            var result = NguoiDungBus.TimNguoiDungTheoTen(ten_dang_nhap, tong_so_dong);
            json = JsonConvert.SerializeObject(result, Formatting.Indented,
new JsonSerializerSettings
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
            return json;
        }

        [WebMethod]
        public string TimNhomSanPhamTheoTen(string ten_nhom_san_pham, int tong_so_dong)
        {
            string json = "";
            var result = NhomSanPhamBus.TimNhomSanPhamTheoTen(ten_nhom_san_pham, tong_so_dong);
            json = JsonConvert.SerializeObject(result, Formatting.Indented,
new JsonSerializerSettings
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
            return json;
        }

        [WebMethod]
        public string TimChucNangTheoTen(string ten_chuc_nang)
        {
            string json = "";
            var result = ChucNangBus.TimChucNangTheoTen(ten_chuc_nang);
            json = JsonConvert.SerializeObject(result, Formatting.Indented,
new JsonSerializerSettings
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
            return json;
        }

        [WebMethod]
        public string TimKhoHangTheoTen(string ten_kho_hang)
        {
            string json = "";
            var result = KhoHangBus.TimKhoHangTheoTen(ten_kho_hang);
            json = JsonConvert.SerializeObject(result, Formatting.Indented,
new JsonSerializerSettings
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
            return json;
        }

        [WebMethod]
        public string TimPhongBanTheoTen(string ten_phong_ban, int tong_so_dong)
        {
            string json = "";
            var result = PhongBanBus.TimPhongBanTheoTen(ten_phong_ban, tong_so_dong);
            json = JsonConvert.SerializeObject(result, Formatting.Indented,
new JsonSerializerSettings
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
            return json;
        }

        [WebMethod]
        public string TimNhaCungCapTheoTen(string ten_nha_cung_cap, int tong_so_dong)
        {
            string json = "";
            var result = NhaCungCapBus.TimNhaCungCapTheoTen(ten_nha_cung_cap, tong_so_dong);
            json = JsonConvert.SerializeObject(result, Formatting.Indented,
new JsonSerializerSettings
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
            return json;
        }

        [WebMethod]
        public string TimNhanVienTheoTen(string ten_nhan_vien, int tong_so_dong)
        {
            string json = "";
            var result = NhanVienBus.TimNhanVienTheoTen(ten_nhan_vien, tong_so_dong);
            json = JsonConvert.SerializeObject(result, Formatting.Indented,
new JsonSerializerSettings
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
            return json;
        }

        [WebMethod]
        public string TimKhachHangTheoTen(string ten_khach_hang, int tong_so_dong)
        {
            string json = "";
            var result = KhachHangBus.TimKhachHangTheoTen(ten_khach_hang, tong_so_dong);
            json = JsonConvert.SerializeObject(result, Formatting.Indented,
new JsonSerializerSettings
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
            return json;
        }

        [WebMethod]
        public string TimSanPhamTheoTen(string ten_san_pham, int tong_so_dong)
        {
            string json = "";
            var result = SanPhamBus.TimSanPhamTheoTen(ten_san_pham, tong_so_dong);
            json = JsonConvert.SerializeObject(result, Formatting.Indented,new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            });
            return json;
        }

        [WebMethod]
        public string TimPhieuNhapTheoMa(string ma_phieu_nhap, int tong_so_dong)
        {
            string json = "";
            var result = PhieuNhapBus.TimPhieuNhapTheoMa(ma_phieu_nhap, tong_so_dong);
            json = JsonConvert.SerializeObject(result, Formatting.Indented,
new JsonSerializerSettings
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
            return json;
        }

        [WebMethod]
        public string TimPhieuXuatTheoMa(string ma_phieu_xuat, int tong_so_dong)
        {
            string json = "";
            var result = PhieuXuatBus.TimPhieuXuatTheoMa(ma_phieu_xuat, tong_so_dong);
            json = JsonConvert.SerializeObject(result, Formatting.Indented,
new JsonSerializerSettings
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
            return json;
        }

        [WebMethod]
        public string TimPhieuXuatTheoNgay(List<DateTime> ngay, int tong_so_dong)
        {
            string json = "";
            var result = PhieuXuatBus.TimPhieuXuatTheoNgay(ngay, tong_so_dong);
            json = JsonConvert.SerializeObject(result, Formatting.Indented,
new JsonSerializerSettings
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
            return json;
        }

        [WebMethod]
        public string LayDanhSachTonKho(DateTime tu_ngay, DateTime den_ngay, string ten_san_pham, string ma_kho_hang)
        {
            string json = "";
            var result = TonKhoBus.LayDanhSachTonKho(tu_ngay, den_ngay, ten_san_pham, ma_kho_hang);
            json = JsonConvert.SerializeObject(result, Formatting.Indented,
new JsonSerializerSettings
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
            return json;
        }

        [WebMethod]
        public string LayQuyenTheoMaND(string ma_nguoi_dung, int ma_chuc_nang)
        {
            string json = "";
            var result = QuyenNhanVienBus.LayQuyenTheoMaND(ma_nguoi_dung, ma_chuc_nang);
            if (result == null)
            {
                json = new JavaScriptSerializer().Serialize(new { SO_QUYEN = 0 });
            }
            else
                json = new JavaScriptSerializer().Serialize(new { SO_QUYEN = result.SO_QUYEN });

            return json;
        }

        [WebMethod]
        public string CapNhatQuyen(string ma_nguoi_dung, int ma_chuc_nang, int so_quyen)
        {
            string json = "";
            int S = -1;

            var obj = QuyenNhanVienBus.CapNhatQuyen(ma_nguoi_dung, ma_chuc_nang, so_quyen);
            if (obj == "1")
            {
                S = 1;
                obj = "Cập nhật thành công";
            }
            json = new JavaScriptSerializer().Serialize(new { SUCCESS = S, ERROR = obj });
            return json;
        }

        [WebMethod]
        public string XemNhatKy_ser(DateTime tu_ngay, DateTime den_ngay, int so_dong)
        {
            string json = "";
            var result =XemNhatKyBus.XemNhatKyNhapXuat_Data(tu_ngay,den_ngay,so_dong);
            json = JsonConvert.SerializeObject(result, Formatting.Indented, new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            });
            return json;
        }

    }
}
